/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mx.itschapala.dsw2.vista;

import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
import mx.itschapala.dsw2.biblioteca.bl.CategoriaBlLocal;
import mx.itschapala.dsw2.biblioteca.modelo.Categoria;

/**
 *
 * @author Santiago
 */
@ManagedBean
@SessionScoped
public class CategoriaBean {
    //-------------------Variables(propiedades)-------
    @EJB
    private CategoriaBlLocal categoriaBl;
    
    private Categoria category;
    private List<Categoria> lista;
    private String accion;
    //-------------------Constructor-------

    public CategoriaBean() {
        category = new Categoria();
    }
    //-------------------metodos GET y SET-------

    public List<Categoria> getLista() {
        lista = categoriaBl.getAll();
        return lista;
    }

    public Categoria getCategoria() {
        return category;
    }

    public void setCategory(Categoria category) {
        this.category = category;
    }
    
    
    //-------------------Acciones-------
    public String procesarAccion() {
        System.out.println("accion categoria>>>>" + accion);
        if (accion.equalsIgnoreCase("Nuevo")) {
            categoriaBl.register(category);
        } else if (accion.equalsIgnoreCase("Editar")) {
            categoriaBl.modify(category);
        } else if (accion.equalsIgnoreCase("Eliminar")) {
            categoriaBl.delete(category);
        }
        accion = "";
        return "Categoria";
    }
    //-------------------Listener-------

    public void prepararNuevo(ActionEvent evt) {
        category = new Categoria();
        accion = "Nuevo";
    }

    public void prepararEditar(ActionEvent evt) {
        System.out.println(evt.getComponent().getClientId());
        String[] arreglo = evt.getComponent().getClientId().split(":");
        String num = arreglo[2];
        int index = Integer.parseInt(num);
        category = lista.get(index);
        accion = "Editar";
        // int index = Integer.parseInt(evt.getComponent().getClientId().split(":")[2]);
    }

    public void prepararEliminar(ActionEvent evt) {
        System.out.println(evt.getComponent().getClientId());
        String[] arreglo = evt.getComponent().getClientId().split(":");
        String num = arreglo[2];
        int index = Integer.parseInt(num);
        category = lista.get(index);
        accion = "Eliminar";
    }
    
}
